<!DOCTYPE html>
            
<HTML>
<HEAD>
<meta name="booktitle" content="Developing Applications With Objective Caml" >
 <meta charset="ISO-8859-1"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<META name="GENERATOR" content="hevea 1.05-7 of 2000-02-24">
<META NAME="Author" CONTENT="Christian.Queinnec@lip6.fr">
<LINK rel=stylesheet type="text/css" href="videoc-ocda.css">
<script language="JavaScript" src="videoc.js"><!--
//--></script>
<TITLE>
 Order of Evaluation of Arguments
</TITLE>
</HEAD>
<BODY class="regularBody">
<A HREF="book-ora028.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="book-ora030.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2> Order of Evaluation of Arguments</H2>
<A NAME="sec-ordre"></A>
<A NAME="@concepts108"></A>
In a pure functional language, the order of evaluation of the
arguments does not matter. As there is no modification of memory state
and no interruption of the calculation, there is no risk of the
calculation of one argument influencing another. On the other hand, in
Objective CAML, where there are physically modifiable values and exceptions,
there is a danger in not taking account of the order of evaluation of
arguments. The following example is specific to version 2.04 of
Objective CAML for Linux on Intel hardware:


<PRE><BR># <B>let</B><CODE> </CODE>new_print_string<CODE> </CODE>s<CODE> </CODE><CODE>=</CODE><CODE> </CODE>print_string<CODE> </CODE>s;<CODE> </CODE>String.length<CODE> </CODE>s<CODE> </CODE>;;<BR><CODE>val new_print_string : string -&gt; int = &lt;fun&gt;</CODE><BR># <TT>(</TT><CODE>+</CODE><TT>)</TT><CODE> </CODE><TT>(</TT>new_print_string<CODE> </CODE><CODE>"Hello "</CODE><TT>)</TT><CODE> </CODE><TT>(</TT>new_print_string<CODE> </CODE><CODE>"World!"</CODE><TT>)</TT><CODE> </CODE>;;<BR><CODE>World!Hello - : int = 12</CODE><BR>

</PRE>

The printing of the two strings shows that the second string is
output before the first. <BR>
<BR>
It is the same with exceptions:


<PRE><BR># <B>try</B><CODE> </CODE><TT>(</TT>failwith<CODE> </CODE><CODE>"function"</CODE><TT>)</TT><CODE> </CODE><TT>(</TT>failwith<CODE> </CODE><CODE>"argument"</CODE><TT>)</TT><CODE> </CODE><B>with</B><CODE> </CODE>Failure<CODE> </CODE>s<CODE> </CODE>-&gt;<CODE> </CODE>s;;<BR><CODE>- : string = "argument"</CODE><BR>

</PRE>
<BR>
<BR>
If you want to specify the order of evaluation of arguments, you have
to make local declarations forcing this order before calling the
function. So the preceding example can be rewritten like this:


<PRE><BR># <B>let</B><CODE> </CODE>e1<CODE> </CODE><CODE>=</CODE><CODE> </CODE><TT>(</TT>new_print_string<CODE> </CODE><CODE>"Hello "</CODE><TT>)</TT><CODE> </CODE><BR><CODE> </CODE><B>in</B><CODE> </CODE><B>let</B><CODE> </CODE>e2<CODE> </CODE><CODE>=</CODE><CODE> </CODE><TT>(</TT>new_print_string<CODE> </CODE><CODE>"World!"</CODE><TT>)</TT><CODE> </CODE><BR><CODE> </CODE><B>in</B><CODE> </CODE><TT>(</TT><CODE>+</CODE><TT>)</TT><CODE> </CODE>e1<CODE> </CODE>e2<CODE> </CODE>;;<BR><CODE>Hello World!- : int = 12</CODE><BR>

</PRE>
<BR>
<BR>
In Objective CAML, the order of evaluation of arguments is not specified. As
it happens, today all implementations of Objective CAML evaluate arguments
from left to right. All the same, making use of this implementation
feature could turn out to be dangerous if future versions of the
language modify the implementation.<BR>
<BR>
We come back to the eternal debate over the design of
languages. Should certain features of the language be deliberately
left unspecified---should programmers be asked not to use them, on
pain of getting different results from their program according to the
compiler implementation? Or should everything be specified---should
programmers be allowed to use the whole language, at the price of
complicating compiler implementation, and forbidding certain
optimizations?<BR>
<BR>
<HR>
<A HREF="book-ora028.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="book-ora030.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
